// haojk/pages/manageSubbranch/index.js
var app = getApp()
Page({
    data: {

        userListInfo: [{
            icon: '../../images/iconfont-dingdan.png',
            text: '提交订单',
            url: '../submitOrder/index'
        }, {
            icon: '../../images/iconfont-dingdan.png',
            text: '我的订单',
            url: '../myorder/index'
        },
        {
            icon: '../../images/iconfont-card.png',
            text: '我的伙伴',
            url: '../partner/index'
        }, {
            icon: '../../images/iconfont-grade.png',
            text: '我的等级',
            url: '../grade/index'
        }, {
            icon: '../../images/iconfont-icontuan.png',
            text: '预估收入',
            url: '../estimate-income/index'
        }, {
            icon: '../../images/footer-icon-03.png',
            text: '结算中心',
            url: '../caculate-credit/index'
        }, {
            icon: '../../images/iconfont-code.png',
            text: '推荐二维码',
            url: '../recommend-code/index'
        }, {
            icon: '../../images/iconfont-help.png',
            text: '新手指引',
            url: '../newuser/index'
        }],



        credit1: "0.00",
        credit2: "0.00",
        credit3: "0.00",
        type: 0,
        pid: "",
        invitepeople: "无",
    },

    openwincommission: function (e) {
        wx.setStorage({
            key: "sendType",
            data: e.currentTarget.dataset.type
        });
        wx.navigateTo({
            url: '../commission/index'
        })



    },

    onLoad: function () {
        var that = this
        var memberinfo = wx.getStorageSync("memberinfo");
        that.setData({ memberinfo: memberinfo});
      
        that.getUserInfor();
        that.getMember();
        that.getOrdersIncomeStatistics();


        //调用应用实例的方法获取全局数据
        app.getUserInfo(function (userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            })
        })
        wx.showShareMenu({
            withShareTicket: true
        })


        wx.getStorage({
            key: "memberinfo",
            success: function (res) {
              
                that.setData({
                    memberid: res.data.memberid,
                    invitepeople: res.data.from_nickname,
                    type: res.data.type,
                    pid:res.data.pid
                });
            }
        });



        //取会员的globalset
        wx.getStorage({
            key: 'globalset',
            success: function (res) {
              
                that.setData({
                    globalset: res.data
                });
                if (that.data.type != 0 || that.data.globalset.isopen_subsidy != 1) {
                    var newuserListInfo = that.data.userListInfo;
                    newuserListInfo.splice(0, 1);
                  
                    that.setData({ userListInfo: newuserListInfo })
                }
            },
        });


    },

    getUserInfor: function () {
        var $that = this;
     
        app.util.request({
            url: 'entry/wxapp/AddMember',
            showLoading: false,
            data: {
                "m": 'nets_haojk',
                // "from_uid": e.data
                "from_uid": "0"
            },
            method: 'post',
            success: function (response) {
             
                wx.setStorage({
                    key: "myUserinformation",
                    data: response.data.data
                });

              
                var obj = response.data.data;
                $that.setData({
                    credit1: obj.credit1 == null ? 0 : obj.credit1,
                    credit2: obj.credit22,
                    type: obj.type,
                    pid: obj.pid
                })
            }
        });
    },

    //顶部分享
    onShareAppMessage: function (res) {
        if (res.from === 'button') {
            // 来自页面内转发按钮
          
        }
       
        return {
            title: this.data.globalset.title,
            path: '/haojk/pages/index/index?from_uid=' + this.data.memberid,
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },
    applyvip: function () {
        var $that = this;
        app.util.request({
            url: 'entry/wxapp/Applyleader',
            data: {
                "m": 'nets_haojk'
            },
            method: 'post',
            success: function (response) {
               
                var obj = response.data.data;
                wx.setStorageSync("applyleader_fee", obj.applyleader_fee);
               
                if (obj.code == 300) {
                    wx.navigateTo({
                        url: "../apply-vip/index"
                    });
                } else {
                    $that.setData({
                        type: 1
                    });
                    //首页存储会员对象到缓存，其他页面直接读取缓存即可
                    wx.setStorage({
                        key: "memberinfo",
                        data: response.data.data
                    });
                    $that.setData({
                        memberid: response.data.data.memberid,
                        datalock: false
                    });
                    wx.showModal({
                        title: '恭喜您，申请成功',
                        content: '您可以通过转发、生成二维码的方式，邀请好友进入小程序网购赚佣金',
                        success: function (res) {
                            if (res.confirm) {
                                //wx.reLaunch({
                                //  url: "../my/index"
                                //});
                            } else if (res.cancel) {
                                //wx.reLaunch({
                                //  url: "../my/index"
                                //});
                            }
                        }
                    });
                }
            }
        });
        /*
        wx.navigateTo({
          url: "../apply-vip/index"
        })
        */
    }
    , getMember: function () {

        var that = this;
        app.getUserInfo(function (userInfo) {
          
            //更新数据
            app.util.getUserInfo(function (response) {

                response.memberInfo.nickname = userInfo.nickName;
                response.memberInfo.avatar = userInfo.avatarUrl;
                response.memberInfo.city = userInfo.city;
                response.memberInfo.country = userInfo.country;
                response.memberInfo.province = userInfo.province;
                response.memberInfo.gender = userInfo.gender;
                that.addMember(response.memberInfo);
            });
        });
        // 页面渲染完成
    },
    addMember: function (userInfo) {
        var $this = this;
        app.util.request({
            url: 'entry/wxapp/AddMember',
            showLoading: false,
            data: {
                "m": 'nets_haojk',
                "from_uid": this.data.from_uid,
                "nickname": userInfo.nickname,
                "avatar": userInfo.avatar,
                "city": userInfo.city,
                "country": userInfo.country,
                "province": userInfo.province,
                "gender": userInfo.gender,
            },
            method: 'post',
            success: function (response) {
              
                //首页存储会员对象到缓存，其他页面直接读取缓存即可
                wx.setStorageSync("memberinfo", response.data.data);

                //取会员的globalset
                wx.getStorage({
                    key: 'memberinfo',
                    success: function (res) {
                       
                        $this.setData({
                            type: res.data.type,
                            level: res.data.level,

                        });
                    },
                });





            }
        });
    },
    getOrdersIncomeStatistics:function(){
        var $that = this;
        
        app.util.request({
            url: 'entry/wxapp/GetOrdersIncomeStatistics',
            showLoading: true,
            data: {
                "m": 'nets_haojk'
            },
            
            method: 'post',
            success: function (response) {
              
                var incomeStatistics = response.data.data;
                $that.setData({ incomeStatistics: incomeStatistics})

            }
        })

    },
    browseShop: function () {

        var memberinfo = wx.getStorageSync("memberinfo");


        wx.switchTab({
            url: '../index/index?from_uid=' + memberinfo.memberid + '&from_nickname=' + memberinfo.nickname + '&type=' + memberinfo.type + '&homeskinid=' + memberinfo.homeskinid,

            success: function (e) {
                var page = getCurrentPages().pop();
                if (page == undefined || page == null) return;
                page.onLoad();
            }
        })
    }
})